//
//  QZBackListVC.m
//  daojiVideo
//
//  Created by tikeios_002 on 2024/5/15.
//

#import "QZBackListVC.h"
#import "QZFansUserModel.h"
#import "QZBackListCell.h"

@interface QZBackListVC ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UITableView *backTableView;
@property (strong,nonatomic)NSMutableArray <QZFansUserModel *>* listArr;
@property (assign,nonatomic) CGFloat itemWdith;
@property (assign,nonatomic) NSInteger page;
@property (strong,nonatomic) PublicView * nullView;
@end

@implementation QZBackListVC

-(void)showTextViewNavViewTo1{
    NSMutableArray * showTextViewNavViewNameArrTo1 = [NSMutableArray new];
    UIView * showTextViewNavViewViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    showTextViewNavViewViewTo1.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:showTextViewNavViewViewTo1];
    UILabel * showTextViewNavViewLabelTo1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    NSInteger showTextViewNavViewNumberTo1 = arc4random_uniform(20);
    NSLog(@"：%ld", (long)showTextViewNavViewNumberTo1);
    showTextViewNavViewLabelTo1.text = [NSString stringWithFormat:@"%ld",(long)showTextViewNavViewNumberTo1];
    [showTextViewNavViewLabelTo1 addSubview:showTextViewNavViewLabelTo1];
    
    UIImageView * showTextViewNavViewLabelTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [showTextViewNavViewViewTo1 addSubview:showTextViewNavViewLabelTo2];
    
}
- (void)showOldAddNavViewToMainOldView1{
    UIView * oldBgSS = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    oldBgSS.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:oldBgSS];
    
    UIButton * sendBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreen_W-75, kStatusBarHeight+10, 60, 46)];
    sendBtn.layer.cornerRadius = 3;
    sendBtn.backgroundColor = [UIColor whiteColor];
    [sendBtn setTitle:@"发送" forState:UIControlStateNormal];
    [sendBtn setTitleColor:ColorHex(@"#333333") forState:UIControlStateNormal];
    sendBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [sendBtn addTarget:self action:@selector(sendTap) forControlEvents:UIControlEventTouchUpInside];
    [oldBgSS addSubview:sendBtn];
    
    UIView * sendBg = [[UIView alloc]initWithFrame:CGRectMake(15, kStatusBarHeight+66, KScreen_W-30, (KScreen_H-KSafeAreaBottomHeight-25-(kStatusBarHeight+66))/2)];
    sendBg.backgroundColor = [UIColor whiteColor];
    sendBg.layer.cornerRadius = 3;
    [oldBgSS addSubview:sendBg];
    
    UILabel * sendTip = [[UILabel alloc]initWithFrame:CGRectMake(0, (sendBg.frame.size.height-40)/2, sendBg.frame.size.width, 40)];
    sendTip.text = @"发送区";
    sendTip.textColor = ColorHex(@"#f0f0f0");
    sendTip.textAlignment = NSTextAlignmentCenter;
    sendTip.font = [UIFont systemFontOfSize:28];
    [sendBg addSubview:sendTip];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = NSLocalizedString(@"黑名单", nil);
    self.view.backgroundColor = viewback_black;
    
    self.itemWdith = (KScreen_W-2)/2;
    KWeakSelf
    self.backTableView.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        [weakSelf.backTableView.mj_footer resetNoMoreData];
        [weakSelf.listArr removeAllObjects];
        [weakSelf.backTableView reloadData];
        weakSelf.page = 0;
        [weakSelf getBackUserList];
    }];
    self.backTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        weakSelf.page ++;
        [weakSelf getBackUserList];
    }];
    

    
    [SVProgressHUDTool showWithStatus:nil];
    [self getBackUserList];
    
    [[QZDataUpdate share] addListenerObj:self];
    
    UILabel * keyLabel = [[UILabel alloc] init];
    keyLabel.textColor = [UIColor grayColor];
    keyLabel.text = @"key";
    keyLabel.textAlignment = NSTextAlignmentCenter;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:keyLabel];
    
    UILabel * valueLabel = [[UILabel alloc] init];
    valueLabel.textColor = [UIColor grayColor];
    valueLabel.text = @"value";
    valueLabel.textAlignment = NSTextAlignmentCenter;
       UILabel * oldshowSSSSTip2 = [[UILabel alloc]init];
    oldshowSSSSTip2.clipsToBounds = YES;
    oldshowSSSSTip2.text = @"kass显示区";
    oldshowSSSSTip2.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip2.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip2.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip2.hidden = YES;
    [self.view addSubview:oldshowSSSSTip2];
    [self.view addSubview:valueLabel];
    
    UITextView * keyTextView1 = [[UITextView alloc] init];
    keyTextView1.backgroundColor = [UIColor whiteColor];
    keyTextView1.font = [UIFont systemFontOfSize:17];
    keyTextView1.layer.borderColor = [UIColor grayColor].CGColor;
    keyTextView1.layer.borderWidth = 1.0f;
    keyTextView1.layer.cornerRadius = 6.0f;
       UILabel * oldshowSSSSTip5 = [[UILabel alloc]init];
    oldshowSSSSTip5.clipsToBounds = YES;
    oldshowSSSSTip5.text = @"kass显示区";
    oldshowSSSSTip5.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip5.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip5.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip5.hidden = YES;
    [self.view addSubview:oldshowSSSSTip5];
    [self.view addSubview:keyTextView1];
    
    UITextView * valueTextView1 = [[UITextView alloc] init];
    valueTextView1.backgroundColor = [UIColor whiteColor];
    valueTextView1.font = [UIFont systemFontOfSize:17];
    valueTextView1.layer.borderColor = [UIColor grayColor].CGColor;
    valueTextView1.layer.borderWidth = 1.0f;
    valueTextView1.layer.cornerRadius = 6.0f;
       UILabel * oldshowSSSSTip3 = [[UILabel alloc]init];
    oldshowSSSSTip3.clipsToBounds = YES;
    oldshowSSSSTip3.text = @"kass显示区";
    oldshowSSSSTip3.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip3.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip3.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip3.hidden = YES;
    [self.view addSubview:oldshowSSSSTip3];
    [self.view addSubview:valueTextView1];
    
    UITextView * keyTextView2 = [[UITextView alloc] init];
    keyTextView2.backgroundColor = [UIColor whiteColor];
    keyTextView2.font = [UIFont systemFontOfSize:17];
    keyTextView2.layer.borderColor = [UIColor grayColor].CGColor;
    keyTextView2.layer.borderWidth = 1.0f;
    keyTextView2.layer.cornerRadius = 6.0f;
       UILabel * oldshowSSSSTip4 = [[UILabel alloc]init];
    oldshowSSSSTip4.clipsToBounds = YES;
    oldshowSSSSTip4.text = @"kass显示区";
    oldshowSSSSTip4.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip4.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip4.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip4.hidden = YES;
    [self.view addSubview:oldshowSSSSTip4];
    [self.view addSubview:keyTextView2];
}
#pragma mark 数据更新
-(void)delObjWithId:(id)delId type:(QZDataUpdateType)type {
   if (delId == nil) return;
    if(type == QZDataUpdateBlock){
        for (QZFansUserModel * model in self.listArr) {
            if([model.mid intValue] == [delId intValue]){
                model.isback = YES;
                [self.backTableView reloadData];
                return;
            }
        }
    }else if (type == QZDataUpdateCancelBlock){
        for (QZFansUserModel * model in self.listArr) {
            if([model.mid intValue] == [delId intValue]){
                model.isback = NO;
                [self.backTableView reloadData];
                return;
            }
        }
    }
}
#pragma mark 懒加载
- (NSMutableArray *)showTextViewToKey1:(NSString *)keyString{
    NSMutableArray *showTextViewArrTo1 = [NSMutableArray new];
    for (int i = 0; [showTextViewArrTo1 count] < 5; i++) {
        int  RandomNumber = arc4random() % 30;
        if(![showTextViewArrTo1 containsObject:[NSNumber numberWithInt:RandomNumber]]){
            [showTextViewArrTo1 addObject:[NSNumber numberWithInt:RandomNumber]];
        }
    }
    return showTextViewArrTo1;
}
- (NSInteger)showTextViewToTime1:(NSString *)time{
    NSDateFormatter *showTextViewFaTo1 = [[NSDateFormatter alloc] init];
    [showTextViewFaTo1 setDateFormat:@"yyyy-MM-dd"];
    return [[showTextViewFaTo1 dateFromString:time] timeIntervalSince1970];
}
- (NSMutableArray *)listArr{
    if(_listArr == nil){
        _listArr = [NSMutableArray new];
    }
    return _listArr;
}
- (UITableView *)backTableView{
    if(_backTableView == nil){
        _backTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreen_W, KScreen_H-KNav_H)];
        _backTableView.backgroundColor = [UIColor clearColor];
        _backTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _backTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _backTableView.delegate = self;
        _backTableView.dataSource = self;
           UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:_backTableView];
    }
    return _backTableView;
}
- (PublicView *)nullView{
    if(_nullView == nil){
        _nullView = [[PublicView alloc]initWithFrame:CGRectMake(0, (KScreen_H-KNav_H-200)/2, KScreen_W, 200)];
        _nullView.publicLabel.text = @"暂无拉黑用户";
        _nullView.hidden = YES;
        _nullView.userInteractionEnabled = NO;
           UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:_nullView];
    }
    return _nullView;
}
#pragma mark 请求区域

- (void)getBackUserList{
    KWeakSelf
    KMutableDic(sendic)
    sendic[@"page"] = @(self.page);
    [YBNetBase userBlackListRequest:sendic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        if(code == 200){
            for (NSDictionary * dic in data[@"list"]) {
                QZFansUserModel * model = [[QZFansUserModel alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                model.isback = YES;
                [weakSelf.listArr addObject:model];
            }
            [weakSelf.backTableView reloadData];

            if([weakSelf.backTableView.mj_header isRefreshing]){
                [weakSelf.backTableView.mj_header endRefreshing];
            }else if ([weakSelf.backTableView.mj_footer isRefreshing]){
                if([data[@"list"] count] == 0){
                    [weakSelf.backTableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    [weakSelf.backTableView.mj_footer endRefreshing];
                }
            }else{
                if(weakSelf.listArr.count == 0){
                    weakSelf.nullView.hidden = NO;
                    weakSelf.backTableView.mj_footer.hidden = YES;
                }else{
                    weakSelf.nullView.hidden = YES;
                    weakSelf.backTableView.mj_footer.hidden = NO;
                }
            }
            
            [SVProgressHUDTool dismiss];
        }else{
            if([weakSelf.backTableView.mj_header isRefreshing]){
                [weakSelf.backTableView.mj_header endRefreshing];
            }else if ([weakSelf.backTableView.mj_footer isRefreshing]){
                weakSelf.page = weakSelf.page-1;
                [weakSelf.backTableView.mj_footer endRefreshing];
            }
            [YBMsgPop showPop:msg];
        }
    }];
}
-(void)showTextViewAddShowViewTo2{
    UIView * showTextViewViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 20, 100, 360)];
    showTextViewViewTo1.backgroundColor = [UIColor whiteColor];
    showTextViewViewTo1.layer.cornerRadius = 8;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:showTextViewViewTo1];
    
    UIImageView * showTextViewimgTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(K_AUTOLAYOUT_W(157.5), K_AUTOLAYOUT_W(260), K_AUTOLAYOUT_W(60), K_AUTOLAYOUT_W(60))];
    showTextViewimgTo2.layer.cornerRadius = K_AUTOLAYOUT_W(30);
    showTextViewimgTo2.clipsToBounds = YES;
    showTextViewimgTo2.contentMode = UIViewContentModeScaleAspectFill;
    showTextViewimgTo2.hidden = YES;
    [showTextViewViewTo1 addSubview:showTextViewimgTo2];
    
    UILabel *  showTextViewimgTo3 = [[UILabel alloc]initWithFrame:CGRectMake(K_AUTOLAYOUT_W(15), K_AUTOLAYOUT_W(35), K_AUTOLAYOUT_W(345), K_AUTOLAYOUT_W(25))];
    showTextViewimgTo3.font = FontBold(18);
    showTextViewimgTo3.textColor = ThreeColor;
    showTextViewimgTo3.textAlignment = NSTextAlignmentCenter;
    showTextViewimgTo3.hidden = YES;
    [showTextViewViewTo1 addSubview:showTextViewimgTo3];
}
- (NSString *)showTextViewGetNowTimerTo3{
    NSDateFormatter *showTextViewGetNowFa = [[NSDateFormatter alloc] init];
    [showTextViewGetNowFa setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    return [showTextViewGetNowFa stringFromDate:[NSDate date]];
}

- (void)setBlock:(NSIndexPath *)indexpath{
    KWeakSelf
    KMutableDic(sendDic)
    [sendDic setObject:self.listArr[indexpath.row].mid forKey:@"tomid"];
    [YBNetBase userBlackUserRequest:sendDic userid:self.listArr[indexpath.row].mid target:weakSelf successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        if(code == 200){
            weakSelf.listArr[indexpath.row].isback = YES;
            [weakSelf.backTableView reloadData];
            [YBMsgPop showPop:@"已拉黑"];
        }else{
            [YBMsgPop showPop:msg];
        }
    }];
}
- (void)setCancelBlock:(NSIndexPath *)indexpath{
    KWeakSelf
    KMutableDic(sendDic)
    [sendDic setObject:self.listArr[indexpath.row].mid forKey:@"to_mid"];
    [YBNetBase userCloseBlackUserRequest:sendDic userid:self.listArr[indexpath.row].mid target:weakSelf successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        if(code == 200){
            weakSelf.listArr[indexpath.row].isback = NO;
            [weakSelf.backTableView reloadData];
            [YBMsgPop showPop:@"已取消拉黑"];
        }else{
            [YBMsgPop showPop:msg];
        }
    }];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.listArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QZBackListCell * cell = [tableView dequeueReusableCellWithIdentifier:@"QZBackListCell"];
    if(cell == nil){
        cell = [[QZBackListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"QZBackListCell"];
    }
    cell.backModel = self.listArr[indexPath.row];
    KWeakSelf
    cell.backListTapBlock = ^(NSInteger index) {
        if(index == 0){
            if(weakSelf.listArr[indexPath.row].isback){
                [weakSelf setCancelBlock:indexPath];
            }else{
                [weakSelf setBlock:indexPath];
            }
        }
    };
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 86;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [QZUserInfoModel otherMineControlView:self mineId:self.listArr[indexPath.row].mid videoId:nil];
}


@end
